package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ArticleDTO;
import com.rdc.example.bean.dto.CommentDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.impl.ArticleServiceImpl;
import com.rdc.example.service.impl.CommentServiceImpl;
import com.rdc.example.view.ArticleView;
import com.rdc.example.view.UtilView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author duck
 */
public class ArticleController {

    private static ArticleServiceImpl articleService = new ArticleServiceImpl();
    private static CommentServiceImpl commentServial=new CommentServiceImpl();
    private static ArticleView articlerView= new ArticleView();
    private static UtilView utilView=new UtilView();
    public static ArticleDTO articleDTO = new ArticleDTO();
    static List<ArticleDTO> articleDTOList=new ArrayList<>();
    static List<CommentDTO> commentDTOList=new ArrayList<>();

    public static void main(String[] args) {
        insertArticle(1);
    }

    public static void showListArticleByUserId(Integer userId){
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        try{
        articleDTOList=articleService.listArticleByUserId(userId);
        showArticleList(articleDTOList);
        MainController.currentArticleDTOList=articleDTOList;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showListArticleSortByLikesDesc(){
        try{
            articleDTOList=articleService.listArticleSortByLikesDesc();
            showArticleList(articleDTOList);
            MainController.currentArticleDTOList=articleDTOList;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showListArticleSortByUpdateAtDesc(){
        try{
            articleDTOList=articleService.listArticleSortByUpdatedAtDesc();
            showArticleList(articleDTOList);
            MainController.currentArticleDTOList=articleDTOList;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showArticleAndComment(ArticleDTO dto,List<CommentDTO> list){
        try{
            ArticleView.showArticleAndColumn(dto,list);
            MainController.currentArticleDTO=dto;
            MainController.currentCommentDTOList=list;
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void insertArticle(Integer userId){
        //Result insertArticle(ArticleDTO article)
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        try{
        ArticleDTO articleDTO = new ArticleDTO();
        ArticleView.insertArticle(articleDTO);
        if(articleDTO.getTitle()==null||articleDTO.getContent()==null){
            utilView.showMessage("标题和内容不可为空");
            return;
        }
        articleDTO.setUserId(userId);
        Result result=articleService.insertArticle(articleDTO);
        System.out.println(result.getMessage());

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void updateArticle(List<ArticleDTO> articleDTOList){
        if (articleDTOList.isEmpty()){
            utilView.showMessage("没有文章可操作，空空如也");
            System.out.println(articleDTOList.size());
            return;
        }
        Integer index= utilView.getUserInputInteger("请输入文章序号");
        if ( index > articleDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的文章");
            return;
        }
        try{
            //先获取原来的文章，在修改标题和内容，不然狠狠地报空指针
            ArticleDTO articleDTO=articleDTOList.get(index-1);
            ArticleView.insertArticle(articleDTO);
            if(articleDTO.getId()==null||articleDTO.getContent()==null){
                return;
            }
            Result result=articleService.updateArticle(articleDTO);
            System.out.println(result.getMessage());

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void deleteArticle(List<ArticleDTO> articleDTOList, UserDTO userDTO){
        if (articleDTOList.isEmpty()){
            utilView.showMessage("没有文章可操作，空空如也");
            return;
        }
        Integer index= utilView.getUserInputInteger("请输入文章编号：");
        if ( index > articleDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的文章");
            return;
        }
        ArticleDTO articleDTO=articleDTOList.get(index-1);
        //为管理员（isTop=1）,是自己的文章（article.userId=user.id）,
        if(!articleDTO.getUserId().equals(userDTO.getId())&&!userDTO.getPower()){
            utilView.showMessage("权限不足！");
            return;
        }
        try{
            //CommentServiceImpl commentServiceImpl=new CommentServiceImpl();
            Integer articleId=articleDTOList.get(index-1).getId();
            Result resultArticle=articleService.deleteArticle(articleId);
           // Result resultComment=commentServiceImpl.deleteComment(articleId);
           // if(resultArticle.getCode()==200&&resultComment.getCode()==200){
                utilView.showMessage(resultArticle.getMessage());
            //}
           // else{
               // utilView.showMessage("删除失败");
            //}燃尽了，坐等火腿肠采访
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void turnToArticle(List<ArticleDTO> articleDTOList){
        if (articleDTOList.isEmpty()){
            utilView.showMessage("没有文章可操作，空空如也");
            return;
        }
        Integer index= utilView.getUserInputInteger("请输入文章编号：");
        if ( index > articleDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的文章");
            return;
        }
        try{
            ArticleDTO articleDTO=articleDTOList.get(index-1);
            Integer articleId=articleDTO.getId();
            List<CommentDTO> commentDTOList=commentServial.listCommentByArticleId(articleId);
            MainController.currentArticleDTO=articleDTO;
            MainController.currentCommentDTOList=commentDTOList;
            ArticleView.showArticleAndColumn(articleDTO,commentDTOList);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void addColumn(Integer columnId, Integer userId){
        //先展示user所有的文章，再选择add
        try{
            List<ArticleDTO> articleDTOList=articleService.listArticleByUserId(userId);
            if (articleDTOList.isEmpty()){
                utilView.showMessage("没有文章可操作，空空如也");
                return;
            }
            showArticleList(articleDTOList);
            Integer index= utilView.getUserInputInteger("请输入文章编号：");
            if ( index > articleDTOList.size()  || index < 1) {
                utilView.showMessage("没有对应的文章");
                return;
            }
            try{
                Integer articleId=articleDTOList.get(index-1).getId();
                Result result=articleService.addColumn(articleId,columnId);
                utilView.showMessage(result.getMessage());
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void removeColumn(List<ArticleDTO> articleDTOList ) {
        if (articleDTOList.isEmpty()){
            utilView.showMessage("没有文章可操作，空空如也");
            return;
        }
        Integer index= utilView.getUserInputInteger("请输入文章编号：");
        if ( index > articleDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的文章");
            return;
        }
        try{
            Integer articleId=articleDTOList.get(index-1).getId();
            Result result=articleService.removeColumn(articleId);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void turnTop(List<ArticleDTO> articleDTOList){
        if (articleDTOList.isEmpty()){
            utilView.showMessage("没有文章可操作，空空如也");
            return;
        }
        Integer index= utilView.getUserInputInteger("请输入文章编号：");
        if ( index > articleDTOList.size()  || index < 1) {
            utilView.showMessage("没有对应的文章");
            return;
        }
        try{
            Integer articleId=articleDTOList.get(index-1).getId();
            Result result=articleService.turnTopArticle(articleId);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showArticleList(List<ArticleDTO> articleDTOList){
        if(articleDTOList.isEmpty()){
            utilView.showMessage("没有文章，空空如也");
        }
        for(int i = 0; i < articleDTOList.size(); i++){
            ArticleDTO dto = articleDTOList.get(i);
            ArticleView.showArticle(i,dto);
        }
    }

    public static void searchArticle() {
        try {
            String key = ArticleView.getInputString();
            if (key == null) {
                utilView.showMessage("输入不可为空！");
                return;
            }
            List<ArticleDTO> list = articleService.searchArticlesByName(key);
            if (list.isEmpty()) {
                utilView.showMessage("搜索结果为空");
                return;
            }
            for (int i = 0; i < list.size(); i++) {
                ArticleView.showArticle(i, list.get(i));
            }
            MainController.currentArticleDTOList = list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
